import actionTypes from '../actions/actionTypes'

const initState = {
  isLoading: false,
  list: [{
    id: 1,
    title: 'Lorem ipsum dolor sit amet.',
    desc: 'Lorem ipsum dolor sit amet consectetur adipisicing elit.',
    hasRead: false
  },{
    id: 2,
    title: 'Lorem ipsum dolor sit amet.',
    desc: 'Lorem ipsum dolor sit amet consectetur adipisicing elit. ',
    hasRead: true
  }]
}

export default (state = initState, action) => {
  switch (action.type) {
    case actionTypes.START_NOTIFICATION_POST:
      return {
        ...state,
        isLoading: true
      }
    case actionTypes.FINISH_NOTIFICATION_POST:
      return {
        ...state,
        isLoading: false
      }
    case actionTypes.MARK_NOTIFICATION_AS_READ_BY_ID:
      const newList = state.list.map(item => {
        if (item.id === action.payload.id) {
          // 标记为已读
          item.hasRead = true
        }
        return item
      })
      return {
        ...state,
        list: newList
      }
    case actionTypes.MARK_ALL_NOTIFICATIONS_AS_READ:
      return {
        ...state,
        list: state.list.map(item => {
          // 标记为已读
          item.hasRead = true
          return item
        })
      }
    case actionTypes.RECIVED_NOTIFICATIONS:
      // console.log('state', state)
      // console.log('action', action)
      return {
        ...state,
        list: action.payload.list
      }
    default:
      return state
  }
}